import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

export interface SystemModelState {
  inviteUserVisible: Boolean;
}

export interface SystemModelType {
  namespace: 'system';
  state: SystemModelState;
  effects: {
    query: Effect;
  };
  reducers: {
    // save: Reducer<SystemModelState>;
    // 启用 immer 之后
    changeIUVisible: ImmerReducer<SystemModelState>;
  };
  subscriptions: { steup: Subscription };
}

const SystemModel: SystemModelType = {
  namespace: 'system',

  state: {
    inviteUserVisible: false,
  },

  effects: {
    *query({ payload }, { call, put }) {},
  },
  reducers: {
    // save(state, action) {
    //   return {
    //     ...state,
    //     ...action.payload,
    //   };
    // },
    // 启用 immer 之后
    changeIUVisible(state, action) {
      console.log('changeIUVisible', state, action);
      state.inviteUserVisible = action.visible;
    },
  },
  subscriptions: {
    steup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'query',
          });
        }
      });
    },
  },
};

export default SystemModel;
